package com.sinosoft.ci.patch;

import java.io.File;
import java.io.FileFilter;
import java.util.LinkedList;
import java.util.List;

/**
 * JavaEE Web 应用程序增量补丁条目生成器
 * 
 * @author 韩朝祥
 *
 */
public class JavaEEWebIncrementEntryGenerator {
	private List<String> patchEntry = new LinkedList<String>();
	
	private File appRootDir;
	private File classesDir;
	
	public JavaEEWebIncrementEntryGenerator(File appRootDir){
		if(null == appRootDir){
			throw new NullPointerException("目录不能为 null！");
		}
		
		if(!appRootDir.exists() || !appRootDir.isDirectory()){
			throw new IllegalArgumentException(String.format("%s 不是一个合法目录！", appRootDir.getAbsolutePath()));
		}
		this.appRootDir = appRootDir;
		
		File classesDir = new File(appRootDir,"WEB-INF/classes");
		if(!classesDir.exists() || !classesDir.isDirectory()){
			throw new IllegalArgumentException(String.format("%s 不是一个合法目录！", classesDir.getAbsolutePath()));
		}
		this.classesDir = classesDir;
	}
	
	/**
	 * 以 java 文件作为条目，解析出 class 补丁路径，能够解析出内部类
	 * @param javaEntry
	 */
	public void addJavaEntry(String javaEntry){
		if(null == javaEntry || !javaEntry.toLowerCase().endsWith(".java")){
			throw new IllegalArgumentException(javaEntry + " 不是一个合法的 java 条目！");
		}
		javaEntry = trimSep(javaEntry);
		
		String[] paths = javaEntry.split("[/\\\\]");
		final String className = paths[paths.length - 1].substring(0 , paths[paths.length - 1].length() - 5);
		
		File dir = new File(classesDir,javaEntry).getParentFile();
		if(!dir.exists() || !dir.isDirectory()){
			throw new RuntimeException(String.format("%s 补丁条目生成失败！由于文件目录 %s 没有找到！",
					javaEntry,dir.getAbsolutePath()));
		}
		File[] files = dir.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File file) {
				if(file.getName().equals(className + ".class")){
					return true;
				}
				
				if(file.getName().startsWith(className + "$") &&
						file.getName().endsWith(".class")){
					return true;
				}
				return false;
			}
		});
		if(null == files || files.length < 1){
			throw new RuntimeException(String.format("%s 补丁条目生成失败！由于在 %s 下没有找到任何 class 文件！"
					, javaEntry,dir.getAbsolutePath()));
		}
		
		for(File file : files){
			String e = "WEB-INF/classes/";
			if(javaEntry.indexOf("/") > 0){
				e = e + javaEntry.substring(0 , javaEntry.lastIndexOf("/") + 1) + file.getName();
			}else{
				e = e + file.getName();
			}
			
			if(!patchEntry.contains(e)){
				patchEntry.add(e);
			}
		}
	}
	
	/**
	 * 添加源码路径下的条目
	 * @param classPathEntry
	 */
	public void addClassPathEntry(String classPathEntry){
		if(null == classPathEntry){
			throw new IllegalArgumentException(classPathEntry + "不是一个合法的条目！");
		}
		if(classPathEntry.toLowerCase().endsWith(".java")){
			addJavaEntry(classPathEntry);
			return;
		}
		classPathEntry = trimSep(classPathEntry);
		patchEntry.add("WEB-INF/classes/" + classPathEntry);
	}
	
	public void addClassEntry(String classEntry){
		if(null == classEntry || !classEntry.toLowerCase().endsWith(".class")){
			throw new IllegalArgumentException(classEntry + "不是一个合法的条目！");
		}
		classEntry = trimSep(classEntry);
		patchEntry.add("WEB-INF/classes/" + classEntry);
	}
	
	public void addUIEntry(String uiEntry){
		if(null == uiEntry){
			throw new IllegalArgumentException(uiEntry + "不是一个合法的条目！");
		}
		uiEntry = trimSep(uiEntry);
		patchEntry.add(uiEntry);
	}
	
	public String[] getPatchEntrys(){
		return patchEntry.toArray(new String[patchEntry.size()]);
	}
	
	private String trimSep(String str){
		while(str.startsWith("/") || str.startsWith("\\")){
			str = str.substring(1);
		}
		
		return str.replace("\\", "/");
	}
}
